package com.ctsi.security.manager.impl;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.cache.CommUtilCache;
import com.ctsi.framework.common.startrun.ComponentFactory;
import com.ctsi.security.I_PermisionTypeExtend;
import com.ctsi.security.PermissionTypeSer;
import com.ctsi.security.domain.PermisionTypeProcess;
import com.ctsi.security.domain.PermissionType;
import com.ctsi.security.server.PermisionTypeExtendManage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


public class PermisionTypeTreeCache {
	private static final Logger  log=LoggerFactory.getLogger(PermisionTypeTreeCache.class);
	private PermissionTypeSer permissionTypeService;
	private String adminPartyId = "admin";
	private String adminUserloginId = "admin";
	private static PermisionTypeTreeCache instance;
	private static final CommUtilCache<String, String> permisionTypeCache = new CommUtilCache<String,String>("permisionTypeCache");
	
	public static PermisionTypeTreeCache getInstance(){
		//不可能为0
		if(permisionTypeCache.size()==0){
			instance = null;
		}
		if(instance != null){
			return instance;
		}
		synchronized (PermisionTypeTreeCache.class) {
			if(instance != null){
				return instance;
			}
			
			PermisionTypeTreeCache temp = new PermisionTypeTreeCache();
			temp.permissionTypeService = (PermissionTypeSer) ComponentFactory.getBean("permissionTypeService");
			//构造树
			PermisionTypeProcess root = new PermisionTypeProcess();
			root.setThisType(PermissionTypeSer.ROOT);
			List<PermisionTypeProcess> all = new ArrayList<PermisionTypeProcess>();
			temp.makeTree( root,all);
			
			//形成缓存 缓存方式   key = a,value=0/bb/a
			for(PermisionTypeProcess node:all){
				String key = node.getThisType();
				String value = temp.makePath(node);
				log.info("key {} value {}", key,value);
				permisionTypeCache.put(key,value);
			}
			instance = temp;
		}		
		
		return instance;
	}
	public static void clear(){
		 instance = null;
		 permisionTypeCache.clear();
	}
	
	public String getPermisionPath(String type){
		return permisionTypeCache.get(type);
	}
	public void printCache(){
		if(log.isDebugEnabled()){
			for(String key :permisionTypeCache.getCacheLineKeys()){
				log.debug("key:,value:",key,permisionTypeCache.get(key));
			}
		}
	}
	
	private String makePath(PermisionTypeProcess node){
		PermisionTypeProcess thisNode = node;
		List<String> list = new LinkedList<String>();
		while(!PermissionTypeSer.ROOT.equals(thisNode.getThisType())){
			list.add(0, thisNode.getThisType());
			thisNode = thisNode.getLastNode();
		}
		list.add(0, PermissionTypeSer.ROOT);
		
		StringBuilder sb = new StringBuilder();
		int i=0;
		for(String str:list){
			if(i++!=0){
				sb.append('/');
			}
			sb.append(str);
		}
		
		return sb.toString();
	}
	
	
	private  void makeTree(PermisionTypeProcess root,List<PermisionTypeProcess> all){
		all.add(root);
		I_PermisionTypeExtend ext = null;
		if(UtilValidate.isNotEmpty(root.getInExtends())){
			ext = PermisionTypeExtendManage.getInstance().getPermisionTypeExtendById(root.getInExtends());
		}
		if(ext == null){
			ext = PermisionTypeExtendManage.getInstance().getPermisionTypeExtendByParent(root.getThisType());
			
		}
		String parent = root.getThisType();
		List<PermissionType> localListPermType = null;
		//判断是否具有扩展点
		if(ext !=null){
			localListPermType = ext.getPermissionTypes(parent, adminPartyId, adminUserloginId);
			if(root.getThisType().equals(ext.getRoot())){
				root.setLastId(root.getThisType());
				root.setInExtends(ext.getId());
			}
			if(UtilValidate.isEmpty(localListPermType)){
				root.setInExtends("");
				parent = root.getLastId();
			};
		}
		if(UtilValidate.isEmpty(localListPermType)){
			localListPermType=permissionTypeService.findAllSon(parent);
		}
		for(PermissionType permissionType:localListPermType){
			makeTree(root.createChild(permissionType.getPermissionTypeId()),all);			
		}
		
	}
	
	public void setPermissionTypeService(PermissionTypeSer permissionTypeService) {
		this.permissionTypeService = permissionTypeService;
	}


}
